﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ahi.Entity;
using Ahi.Controller;
using Ahi.Config;
using DevExpress.XtraEditors;
using Ahi.Entity.Enums;
using MongoDB.Bson;
using Ahi.Entity.Interfaces;
using Ahi.Raporlama;


namespace Ahi.Uyelik
{
    public partial class UyeListesiForm : Base.BaseForm
    {
        List<UyeEntity> coll = new List<UyeEntity>();

        public UyeEntity CurrentUye
        {
            get
            {
                if (bsUyeler.Current is UyeEntity)
                    return bsUyeler.Current as UyeEntity;
                return null;
            }
        }

        public UyeListesiForm()
        {
            InitializeComponent();
        }

        public override void Hazirla()
        {
            repositoryItemLookUpAdresTipi.SetEnumList<AdresTipi>();
            repositoryItemLookUpEmailTipi.SetEnumList<EmailTipi>();
            repositoryItemLookUpKanGrubu.SetEnumList<KanGrubu>();
            repositoryItemLookUpTelefonTipi.SetEnumList<TelefonTipi>();
            //repositoryItemLookUpUyeSekli.SetEnumList<UyeSekli>();
            //repositoryItemLookUpBelgeTipi.SetEnumList<BelgeTipi>();
            repositoryItemLookUpTeslimDurumu.SetEnumList<TeslimDurumu>();
            repositoryItemLookUpCinsiyet.SetEnumList<Cinsiyet>();

            repositoryItemLookUpBelgeTipi.Items.Clear();
            repositoryItemLookUpBelgeTipi.NullText = "Seçiniz";
            repositoryItemLookUpBelgeTipi.Items.AddRange(MongoAdaptor.Instance.GetData<UyeEntity.BelgeEntity.BelgeTipiEntity>().ToArray());

            repositoryItemLookUpUyeSekli.Items.Clear();
            repositoryItemLookUpUyeSekli.NullText = "Seçiniz";
            repositoryItemLookUpUyeSekli.Items.AddRange(MongoAdaptor.Instance.GetData<Ahi.Entity.UyeEntity.UyelikTipiEntity>().ToArray());
        }

        public override void Yukle()
        {
            Hazirla();
            Listele();
        }

        private void Listele()
        {
            coll = MongoAdaptor.Instance.GetData<UyeEntity>();
            bsUyeler.DataSource = coll;
        }

        public override void Kaydet()
        {
            MongoAdaptor.Instance.Save<UyeEntity>(coll);
            base.Kaydet();
        }

        private void buttonKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kaydet();
        }

        private void buttonListele_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Listele();
        }

        private void buttonExceleAktar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExceleAktar();
        }

        private void ExceleAktar()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Excel Dosyaları (*.xls)|*.xls";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                gridViewUyeler.ExportToXls(dlg.FileName, true);
            }
        }

        private void buttonYazdir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Yazdir();
        }

        private void Yazdir()
        {
            RaporEntity rapor = null;
            if (RaporSecimForm.ShowRaporSecim(ref rapor, AppConfig.Current.UyelerRaporu) == System.Windows.Forms.DialogResult.OK)
            {
                if (rapor.Id == ObjectId.Empty)
                    gridUyeler.ShowPrintPreview();
                else
                {
                    AppConfig.Current.UyelerRaporu = rapor.Id;
                    AppConfig.Current.SaveToDatabase();

                    Raporlama.RaporGoruntuleyiciForm frm = new Raporlama.RaporGoruntuleyiciForm();
                    frm.Id = AppConfig.Current.UyelerRaporu;
                    if (rapor.KayitGosterimSekli == KayitGosterimSekli.Tekil)
                        frm.DataSource = CurrentUye;
                    else
                        frm.DataSource = bsUyeler.DataSource;
                    frm.Show();
                }
            }
        }

        private void buttonKapat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void gridUyeler_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.Append:
                    break;
                case NavigatorButtonType.CancelEdit:
                    break;
                case NavigatorButtonType.Custom:
                    break;
                case NavigatorButtonType.Edit:
                    break;
                case NavigatorButtonType.EndEdit:
                    break;
                case NavigatorButtonType.First:
                    break;
                case NavigatorButtonType.Last:
                    break;
                case NavigatorButtonType.Next:
                    break;
                case NavigatorButtonType.NextPage:
                    break;
                case NavigatorButtonType.Prev:
                    break;
                case NavigatorButtonType.PrevPage:
                    break;
                case NavigatorButtonType.Remove:
                    if (CurrentUye != null &&
                                XtraMessageBox.Show("Silmek istediğinize emin misiniz?", "Dikkat", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        CurrentUye.Delete();;
                        bsUyeler.RemoveCurrent();
                    }
                    e.Handled = true;
                    break;
                default:
                    break;
            }

        }

        private void buttonDuzenle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Duzenle();
        }

        private void Duzenle(bool yeni = false)
        {
            UyeDuzenlemeForm frm = new UyeDuzenlemeForm();
            if (!yeni)
            {
                if (CurrentUye == null) return;
                frm.Id = CurrentUye.Id;
            }
            frm.ShowDialog();
            Listele();
        }

        private void buttonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Duzenle(true);
        }

        private void gridViewUyeler_DoubleClick(object sender, EventArgs e)
        {
            Duzenle();
        }
    }
}
